package com.test.readData;

import com.test.jdbc.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class readModel {

    public static void storeData(String tableName,Map<String, ArrayList<Double>> modelMap) throws SQLException {

        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        queryRunner.update("create table " + tableName + "_model(num int primary key auto_increment," +
                "W double ,A1 double ,A2 double ,A3 double ,A4 double ,A5 double ," +
                " A6 double ,A7 double ,A8 double ,A9 double ,A10 double ,A11 double ," +
                "A12 double ,A13 double ,A14 double ,A15 double ,A16 double ,D1 double,D2 double" +
                ",D3 double,D4 double,D5 double);");

        for (int i = 0; i < modelMap.size(); i++) {
            queryRunner.update("insert into " + tableName + "_model values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    modelMap.get("r"+ i).get(0),modelMap.get("r"+i).get(1),modelMap.get("r"+i).get(2),modelMap.get("r"+i).get(3),
                    modelMap.get("r"+i).get(4), modelMap.get("r"+i).get(5),modelMap.get("r"+i).get(6),modelMap.get("r"+i).get(7),
                    modelMap.get("r"+i).get(8), modelMap.get("r"+i).get(9),modelMap.get("r"+i).get(10),modelMap.get("r"+i).get(11),
                    modelMap.get("r"+i).get(12), modelMap.get("r"+i).get(13),modelMap.get("r"+i).get(14),modelMap.get("r"+i).get(15),
                    modelMap.get("r"+i).get(16), modelMap.get("r"+i).get(17),modelMap.get("r"+i).get(18),modelMap.get("r"+i).get(19),
                    modelMap.get("r"+i).get(20), modelMap.get("r"+i).get(21));
        }
    }

    public static Map<String, ArrayList<Double>> modelArray(FileReader fileReader ) throws IOException {
        Map<String, ArrayList<Double>> modelMap = new HashMap<String, ArrayList<Double>>();

        BufferedReader br = new BufferedReader(fileReader);
        String line;
        int num=0;
        while ((line=br.readLine())!=null) {
            String[] ss = line.split("\t");
            ArrayList<Double> r = new ArrayList<Double>();
            for(int i=0; i<ss.length; i++){
                r.add(Double.parseDouble(ss[i]));
            }
        modelMap.put("r"+num,r);
        //r.clear();
        num++;
        }
        return modelMap;
    }

}
